package com.qgs.waf.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

public class DateUtil {
	private static SimpleDateFormat sdf;

	enum DateStyle {
		S1 {
			@Override
			String style() {
				return "yyyy-MM-dd HH:mm:ss";
			}
		},
		S2 {
			@Override
			String style() {
				return "yyyy-MM-dd";
			}
		},
		S3 {
			@Override
			String style() {
				return "yyyyMMdd";
			}
		};
		abstract String style();
	}

	/**
	 * 判断时间是否在时间段内
	 * 
	 * @param date
	 *            当前时间 yyyy-MM-dd HH:mm:ss
	 * @param strDateBegin
	 *            开始时间 00:00:00
	 * @param strDateEnd
	 *            结束时间 00:05:00
	 * @return
	 * @throws ParseException
	 */
	public static boolean isInDate(String date, String strDateBegin, String strDateEnd) throws ParseException {
		return isInDate(3, date, strDateBegin, strDateEnd);
	}

	public static boolean isInDate(int dateStyle, String date, String strDateBegin, String strDateEnd) throws ParseException {
		if (null == sdf) {
			String date_format_style = null;
			switch (dateStyle) {
			case 1:
				date_format_style = DateStyle.S1.style();
				break;
			case 2:
				date_format_style = DateStyle.S2.style();
				break;
			case 3:
				date_format_style = DateStyle.S3.style();
				break;
			default:
				break;
			}
			sdf = new SimpleDateFormat(date_format_style);
		}
		Date n_datr = sdf.parse(date);
		Date start = sdf.parse(date);
		Date stop = sdf.parse(date);
		return isInDate(n_datr, start, stop);
	}

	/**
	 * @show 判断时间是否在一区间内
	 * @param date
	 * @param strDateBegin
	 * @param strDateEnd
	 * @return
	 * @throws ParseException
	 */
	public static boolean isInDate(Date date, Date strDateBegin, Date strDateEnd) throws ParseException {
		long d = date.getTime();
		long start = date.getTime();
		long end = date.getTime();
		return ((d <= start) && (d >= end));
	}

	public static TreeSet<String> getTimeSection(String format,int days_time, Date strDateBegin, Date strDateEnd) throws ParseException {
		TreeSet<String> days = new TreeSet<String>();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		int days_num = daysBetween(strDateBegin, strDateEnd);
		for (int i = 0; i <=days_num; i++) {
			days.add(sdf.format(addOneDay(format, strDateBegin,i)));
		}
		return days;
	}

	public static Date addOneDay(String format, Date date,int days_time) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, days_time);
		return cal.getTime();
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		TreeSet<String> days = getTimeSection("yyyy-MM-dd",1, sdf.parse("2015-12-01"), sdf.parse("2015-12-31"));
		for (String string : days) {
			System.out.println(string);
		}
	}
}